package com.asm.slidingmenu;



import com.dspl.asmswd.R;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONObject;

import com.asm.DBloader.ASM_AVTemp;
import com.asm.DBloader.ASM_AVTempDS;
import com.asm.DBloader.ASM_DB;
import com.asm.DBloader.ASM_DBlist;
import com.asm.DBloader.ASM_DBlistDS;
import com.asm.DBloader.ASM_FArea;
import com.asm.DBloader.ASM_FAreaDS;
import com.asm.DBloader.ASM_FBrand;
import com.asm.DBloader.ASM_FBrandDS;
import com.asm.DBloader.ASM_FCompanyBranch;
import com.asm.DBloader.ASM_FCompanyBranchDS;
import com.asm.DBloader.ASM_FItenrDet;
import com.asm.DBloader.ASM_FItenrDetDS;
import com.asm.DBloader.ASM_FLocation;
import com.asm.DBloader.ASM_FLocationDS;
import com.asm.DBloader.ASM_FOrdDet;
import com.asm.DBloader.ASM_FOrdDetDS;
import com.asm.DBloader.ASM_FOrdHed;
import com.asm.DBloader.ASM_FOrdHedDS;
import com.asm.DBloader.ASM_FRoute;
import com.asm.DBloader.ASM_FRouteDS;
import com.asm.DBloader.ASM_Fasm;
import com.asm.DBloader.ASM_FasmDS;
import com.asm.DBloader.ASM_Fdebtor;
import com.asm.DBloader.ASM_FdebtorDS;
import com.asm.DBloader.ASM_FinvDet;
import com.asm.DBloader.ASM_FinvDetDS;
import com.asm.DBloader.ASM_FinvFreeIss;
import com.asm.DBloader.ASM_FinvFreeIssDS;
import com.asm.DBloader.ASM_FinvHed;
import com.asm.DBloader.ASM_FinvHedDS;
import com.asm.DBloader.ASM_Serverinfo;
import com.asm.DBloader.ASM_ServerinfoDS;
import com.asm.DBloader.ASM_Usersettings;
import com.asm.DBloader.ASM_UsersettingsDS;
import com.asm.DBloader.ASM_fASE;
import com.asm.DBloader.ASM_fASEDS;
import com.asm.DBloader.ASM_fCompanySetting;
import com.asm.DBloader.ASM_fCompanySettingDS;
import com.asm.DBloader.ASM_fDealer;
import com.asm.DBloader.ASM_fDealerDS;
import com.asm.DBloader.ASM_fExpense;
import com.asm.DBloader.ASM_fExpenseDS;
import com.asm.DBloader.ASM_fGroup;
import com.asm.DBloader.ASM_fGroupDS;
import com.asm.DBloader.ASM_fInvrdet;
import com.asm.DBloader.ASM_fInvrdetDS;
import com.asm.DBloader.ASM_fInvrhed;
import com.asm.DBloader.ASM_fInvrhedDS;
import com.asm.DBloader.ASM_fItems;
import com.asm.DBloader.ASM_fItemsDS;
import com.asm.DBloader.ASM_fProvince;
import com.asm.DBloader.ASM_fProvinceDS;
import com.asm.DBloader.ASM_fReason;
import com.asm.DBloader.ASM_fReasonDS;
import com.asm.DBloader.ASM_fRegion;
import com.asm.DBloader.ASM_fRegionDS;
import com.asm.DBloader.ASM_fRouteDet;
import com.asm.DBloader.ASM_fRouteDetDS;
import com.asm.DBloader.ASM_fSalRep;
import com.asm.DBloader.ASM_fSalRepDS;

import android.R.drawable;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Handler;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.app.ProgressDialog;
import android.content.res.Configuration;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Message;
import android.os.TransactionTooLargeException;
import android.provider.Settings;
import android.util.Log;
import android.util.Patterns;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

public class ASM_Home_fragment extends Fragment implements OnClickListener {
	
	private String DBNAME = "ASM.db";
	private SQLiteDatabase mydb;
	private String SERVER_DB_LIST = "serverdb";
	private static final int OLDDB_VERSION = 20;
	private static final int DATABASE_VERSION = 21;
	private static final int MODE_PRIVATE = 0;
	public static String DBName1,ASMName,DBnameprf,DownloadDetails,chk;
	Boolean isInternetOK = true;
	int arrayLength = 0;
	String connectionURL;
	ASM_Internetconnection icn;
	ProgressDialog ASMProgressbar,ASMProgressbar1;
	 
		
	
	ASM_DB db;
	ArrayList<ASM_DBlistDS> arrList;
	ArrayList<String> dblistarr;
	
	public ASM_Home_fragment()
	{
		
	}
	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState)
	{

        View rootView = inflater.inflate(R.layout.asm_fragment_home, container, false);
        GPSTracker gps = new GPSTracker(getActivity());
        db = new ASM_DB(getActivity());
    	SQLiteDatabase YourDatabaseName;
		YourDatabaseName = db.getWritableDatabase();
		db.onUpgrade(YourDatabaseName, 20, 21);
		
		SharedPreferences pref1 = getActivity().getSharedPreferences("MyPref", 0); 
		Editor editor = pref1.edit();
		String chk1 = pref1.getString("status", null);
		if(chk1==null)
		{
			editor.putString("status", "ok");
			editor.commit();
			checklogin();
			
		}else if(chk1=="ok")
		{
			//editor.putString("status", "ok");
			//editor.commit();
			FragmentTransaction transaction = getFragmentManager().beginTransaction();
			transaction.replace(R.id.frame_container, new ListViewMultiChartActivity());
			transaction.addToBackStack(null);
			transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
			transaction.commit();
		}else 
		{
			checklogin();
			editor.clear();
			editor.commit();
		}
		
		
        
        return rootView;
    }
	  @Override
	public void onDestroy() 
	  {
	        super.onDestroy();
	        SharedPreferences pref1 = getActivity().getSharedPreferences("MyPref", 0); 
	        Editor editor = pref1.edit();
	        editor.clear();
	        editor.commit();
	        
	        Log.v("Distroy", "Distroy");
	   }
//	  @Override
//		public void onStop() 
//		  {
//		        super.onStop();
//		        SharedPreferences pref1 = getActivity().getSharedPreferences("MyPref", 0); 
//		        Editor editor = pref1.edit();
//		        editor.putString("status", null);
//		        editor.commit();
//		        Log.v("Distroy", "Distroy");
//		   }

	@Override
	public void onClick(View v) 
	{
		// TODO Auto-generated method stub

	}
	
	public void showCustomDialog() {
		// TODO Auto-generated method stub
		//final Dialog dialog = new Dialog(HomeFragment.this);
		final Dialog dialog = new Dialog(getActivity());
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(R.layout.urldialogbox);
		dialog.setCancelable(false);
		
		final EditText editText = (EditText)dialog.findViewById(R.id.etPass);
		final TextView connectionURL1 = (TextView)dialog.findViewById(R.id.etUserName);
		Button button = (Button)dialog.findViewById(R.id.btnSingIn);	
		button.setOnClickListener(new View.OnClickListener()
		{
			
			@SuppressWarnings("static-access")
			@Override
			public void onClick(View v)
			{
				// TODO Auto-generated method stub
				isInternetOK = isConnectingToInternet();
				if(isInternetOK)
				{
					if(Patterns.WEB_URL.matcher(connectionURL1.getText().toString().trim()).matches())
					{
					connectionURL = connectionURL1.getText().toString().trim();
					String kk = connectionURL1.getText().toString().trim();
					ASM_Serverinfo URL = new ASM_Serverinfo();
					ASM_ServerinfoDS URLDS = new ASM_ServerinfoDS(getActivity());
					URL.setASM_SERVERURL(connectionURL1.getText().toString().trim());
					URLDS.Create(URL);
					new DownloadDB().execute();
					dialog.dismiss();
					}
					else
					{
						AlertDialog.Builder builder1 = new AlertDialog.Builder(getActivity());
						builder1.setTitle("ERROR");
						builder1.setIcon(R.drawable.ic_error);
			            builder1.setMessage("Please Enter Valid URL...");
			            builder1.setCancelable(true);
			            builder1.setPositiveButton("Ok",
			                new DialogInterface.OnClickListener()
			            {
			                public void onClick(DialogInterface dg, int id) 
			                {
			                	dg.dismiss();
			           
			                }
			            });
			            AlertDialog alert11 = builder1.create();
			            alert11.show();		
					}
				
				}
				else 
				{
					showSettingsAlert();
				}
			}
		});
				
		dialog.show();
	}
	
	private class DownloadDB extends AsyncTask<Void, Void, Void>
	{
		protected void onPreExecute() 
		{
			super.onPreExecute();
			ASMProgressbar = new ProgressDialog(getActivity());
			ASMProgressbar.setCancelable(false);
			ASMProgressbar.setMessage("Downloading....");
			ASMProgressbar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
			ASMProgressbar.setIndeterminate(false);
			ASMProgressbar.setMax(100);
			ASMProgressbar.show();
			
			
		     Runnable myThread = new Runnable(){
				 
				  @Override
				  public void run() 
				  {	  
					 if (arrayLength<=100)
					 	{

						     try
						     {
						    	 progressHandler.sendMessage(progressHandler.obtainMessage());
						    	Thread.sleep(1);
						    	
						     }
						     catch(Throwable t)
						     {
						    	
						     }
						}
					 
				  }

				};myThread.run();
		}
		
		public int kk = 0;
		@SuppressWarnings("unused")
		@Override
		protected Void doInBackground(Void... params) 
		{
			// TODO Auto-generated method stub
			try 
			{
				String MainUrl = connectionURL
						+ "/PDADBWebServiceM/PDADBWebServiceRest.svc/GetdatabaseNames/mobile123";

				URL json = new URL(MainUrl);
				URLConnection jc = json.openConnection();
				BufferedReader readerfdblist = new BufferedReader(
						new InputStreamReader(jc.getInputStream()));

				String line = readerfdblist.readLine();

				JSONObject jsonResponse = new JSONObject(line);

				JSONArray jsonArray = jsonResponse.getJSONArray("GetdatabaseNamesResult");
				 kk = jsonArray.length();
				for (int i = 0; i < jsonArray.length(); i++) 
				{
					progressHandler.sendMessage(progressHandler.obtainMessage());
					JSONObject jObject = (JSONObject) jsonArray.get(i);
					String sdbname = jObject.getString("Name");
					ContentValues newrowdblist = new ContentValues();
					Cursor sdblistrows = null;
					
					if (sdblistrows == null)
					{	
						arrayLength = arrayLength + 1;
						ASM_DBlist asDblist = new ASM_DBlist();
						ASM_DBlistDS DS = new ASM_DBlistDS(getActivity());
						asDblist.setASM_DBNAME(sdbname.toString().trim());
						DS.Create(asDblist);

		
					} 
					
					else if (sdblistrows.getCount() < 1)
					{
						arrayLength = arrayLength + 1;
						ASM_DBlist asDblist = new ASM_DBlist();
						ASM_DBlistDS DS = new ASM_DBlistDS(getActivity());
						asDblist.setASM_DBNAME(sdbname.trim());
						DS.Create(asDblist);
					}
				}
					
				readerfdblist.close();
			} catch (Exception e) 
			{
				// TODO: handle exception
				Log.v("Error", e.toString());
			}
			return null;
		}
		
		@Override
		protected void onPostExecute(Void args) {
			
				ASMProgressbar.dismiss();
				showasmdblist();
				
			return;
		}

		
		Handler progressHandler = new Handler()
		{
			public void handleMessage(Message msg) 
			{
				ASMProgressbar.incrementProgressBy(arrayLength);
			}
		};
		
	}
	protected void showasmdblist() 
	{
		// TODO Auto-generated method stub
		final Dialog dialog = new Dialog(getActivity());
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(R.layout.asm_fragment_configuration);
		dialog.setCancelable(false);
		
		Button btnDone = (Button)dialog.findViewById(R.id.btnDone);	
		Button btnGet = (Button)dialog.findViewById(R.id.btnget);
		TextView textDBname = (TextView)dialog.findViewById(R.id.textView1);
		TextView textDBview = (TextView)dialog.findViewById(R.id.TextView02);
		
		Spinner dblistspin = (Spinner)dialog.findViewById(R.id.dblistspin);
		final Spinner asmspin = (Spinner)dialog.findViewById(R.id.Spinner01);
		
		SharedPreferences pref = getActivity().getSharedPreferences("MyPref", MODE_PRIVATE); 
	    final Editor editor = pref.edit();
		    
		ASM_DBlistDS ds=new ASM_DBlistDS(getActivity());
		ArrayList<String> list =ds.getAllASM_DBlist();

			ArrayAdapter<String> categoriesAdapter = new ArrayAdapter<String>(getActivity().getApplicationContext(), R.layout.item, list);
			dblistspin.setAdapter(categoriesAdapter);
			
			 DBnameprf =  pref.getString("key_name5", null);
			if(DBnameprf != null)
			{
				dblistspin.setVisibility(View.GONE);
				textDBview.setText(""+DBnameprf+" SELECTED");
				btnGet.setVisibility(View.GONE);
				
				
				
			}
		
			
			dblistspin.setOnItemSelectedListener(new OnItemSelectedListener() 
			{

				@Override
				public void onItemSelected(AdapterView<?> parent, View view,
						int position, long id) {
					// TODO Auto-generated method stub
					 DBName1 = parent.getItemAtPosition(position).toString();
					 editor.putString("key_name5", DBName1);
					 editor.commit();
				
				}

				@Override
				public void onNothingSelected(AdapterView<?> parent) {
					// TODO Auto-generated method stub
					
				}
				
			}); 
			
			
			btnGet.setOnClickListener(new View.OnClickListener() 
			{
				
				@Override
				public void onClick(View v) 
				{
					// TODO Auto-generated method stub
					AlertDialog.Builder builder1 = new AlertDialog.Builder(getActivity());
					builder1.setTitle("ATTENTION");
					builder1.setIcon(R.drawable.ic_attention);
		            builder1.setMessage("Confirm Your Selection...");
		            builder1.setCancelable(true);
		            builder1.setPositiveButton("Yes",
		                    new DialogInterface.OnClickListener() {
		                public void onClick(DialogInterface dg, int id) 
		                {
		                	dg.dismiss();
		                    new DownloadDB2().execute();
							
							ASM_FasmDS dt = new ASM_FasmDS(getActivity());
							ArrayList<String> list1 = dt.getFasm();
							ArrayAdapter<String> categoriesAdapter1 = new ArrayAdapter<String>(getActivity().getApplicationContext(), R.layout.item, list1);
							asmspin.setAdapter(categoriesAdapter1);
							dialog.dismiss();
		                }
		            });
		            builder1.setNegativeButton("No",
		                    new DialogInterface.OnClickListener() 
		            {
		                public void onClick(DialogInterface dialog, int id) 
		                {
		                    dialog.cancel();
		                }
		            });

		            AlertDialog alert11 = builder1.create();
		            alert11.show();
					
					
					
					
					
					
					
					
				}
			});
			
			asmspin.setOnItemSelectedListener(new OnItemSelectedListener() 
			{

				@Override
				public void onItemSelected(AdapterView<?> parent, View view,
						int position, long id) {
					// TODO Auto-generated method stub
					
					ASMName = parent.getItemAtPosition(position).toString();
					
				
				}

				@Override
				public void onNothingSelected(AdapterView<?> parent) {
					// TODO Auto-generated method stub
					
				}
				
			}); 
			ASM_FasmDS dt = new ASM_FasmDS(getActivity());
			ArrayList<String> list1 = dt.getFasm();
			
			
			ArrayAdapter<String> categoriesAdapter1 = new ArrayAdapter<String>(getActivity().getApplicationContext(), R.layout.item, list1);
			asmspin.setAdapter(categoriesAdapter1);
		
		btnDone.setOnClickListener(new View.OnClickListener()  
		{
			
			@Override
			public void onClick(View v)
			{
	
				
				ASM_FasmDS fDS = new ASM_FasmDS(getActivity());
				ArrayList<String> flist = fDS.getFasm();
				ArrayAdapter<String> Fasmnames = new ArrayAdapter<String>(getActivity().getApplicationContext(), R.layout.item, flist);
				int ll = Fasmnames.getCount();
				
				if( Fasmnames.getCount() > 0)
				{
			
					// TODO Auto-generated method stub
					 	new DownloadDB1().execute();
					 	
						ASM_Usersettings asDblist1 = new ASM_Usersettings();
						ASM_UsersettingsDS DS1 = new ASM_UsersettingsDS(getActivity());
						asDblist1.setAsmUSersettings_asmcode(ASMName.toString());
						asDblist1.setAsmUSersettings_serverdbname(DBNAME.toString());
						DS1.Create(asDblist1);
					 	
							dialog.dismiss();
				}else
				{
					
					AlertDialog.Builder builder1 = new AlertDialog.Builder(getActivity());
					builder1.setTitle("ATTENTION");
					builder1.setIcon(R.drawable.ic_attention);
		            builder1.setMessage("Please Get ASM List");
		            builder1.setCancelable(true);
		            builder1.setPositiveButton("Ok",
		                    new DialogInterface.OnClickListener() {
		                public void onClick(DialogInterface dg, int id) 
		                {
		                	dg.dismiss();
		           
		                }
		            });
		            AlertDialog alert11 = builder1.create();
		            alert11.show();
				
				}

			}
		});
				
		dialog.show();
		
	}
	public void Login() 
	{
		final Dialog dialog = new Dialog(getActivity());
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(R.layout.asm_fragment_login);
		
		dialog.setCancelable(false);
		
		ASM_UsersettingsDS ds=new ASM_UsersettingsDS(getActivity());
		final String ASmcode =ds.getASMCode().toString().trim();
		
		//final EditText editText = (EditText)dialog.findViewById(R.id.etPass);
		Button button = (Button)dialog.findViewById(R.id.btnlogin);	
		final TextView etUserName = (TextView)dialog.findViewById(R.id.etUserName);
		final TextView etPass = (TextView)dialog.findViewById(R.id.etPass);
		button.setOnClickListener(new View.OnClickListener() 
		{
			
			@Override
			public void onClick(View v)
			{
				String k = etUserName.getText().toString().trim();
				String k1 = etPass.getText().toString().trim();
				String k12 = ASmcode;

				if(etUserName.getText().toString().equals(ASmcode) && etPass.getText().toString().equals(ASmcode))
				{
					final GPSTracker gps = new GPSTracker(getActivity());
				// TODO Auto-generated method stub
					if(gps.canGetLocation())
					{
						dialog.dismiss();
						FragmentTransaction transaction = getFragmentManager().beginTransaction();
						transaction.replace(R.id.frame_container, new ListViewMultiChartActivity());
						transaction.addToBackStack(null);
						transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
						transaction.commit();
					}
					else 
					{
					gps.showSettingsAlert();
					//Login();
					//dialog.dismiss();
					}
				
				}
				else
				{
					AlertDialog.Builder builder1 = new AlertDialog.Builder(getActivity());
					builder1.setTitle("ERROR");
					builder1.setIcon(R.drawable.ic_error);
		            builder1.setMessage("Please Enter Valid Username and Password...");
		            builder1.setCancelable(true);
		            builder1.setPositiveButton("Ok",
		            new DialogInterface.OnClickListener() 
		            {
		                public void onClick(DialogInterface dg, int id) 
		                {
		                	dg.dismiss();
		           
		                }
		            });
		            AlertDialog alert11 = builder1.create();
		            alert11.show();
				}
			}
		});
				
		dialog.show();
	}
	
	public void checklogin() 
	{
		ASM_DBlistDS ds=new ASM_DBlistDS(getActivity());
		ArrayList<String> list =ds.getAllASM_DBlist();
		ArrayAdapter<String> categoriesAdapter = new ArrayAdapter<String>(getActivity().getApplicationContext(), R.layout.item, list);
		
		if(categoriesAdapter.getCount()<=0)
		{
			showCustomDialog();
		}
		else
		{
			
			Login();
			
		
		}
	}

	public boolean isConnectingToInternet() 
	{

	    ConnectivityManager cm = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
	    NetworkInfo netinfo = cm.getActiveNetworkInfo();

	    if (netinfo != null && netinfo.isConnectedOrConnecting()) {
	        android.net.NetworkInfo wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
	        android.net.NetworkInfo mobile = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

	        if((mobile != null && mobile.isConnectedOrConnecting()) || (wifi != null && wifi.isConnectedOrConnecting())) return true;
	        //else return false;
	    } 
	    
	        return false;
	    
	}
	
	public void showSettingsAlert()
	{
		AlertDialog.Builder alertDialog = new AlertDialog.Builder(getActivity());
   	 
        // Setting Dialog Title
        alertDialog.setTitle("INTERNET SETTINGS");
 
        // Setting Dialog Message
        alertDialog.setMessage("Internet is not enabled. Do you want to go to settings menu?");
 
        // On pressing Settings button
        alertDialog.setPositiveButton("Settings", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog,int which) {
            	Intent intent = new Intent(Settings.ACTION_DATA_ROAMING_SETTINGS);
            	getActivity().startActivity(intent);
            }
        });
 
        // on pressing cancel button
        alertDialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();
            }
        });
 
        // Showing Alert Message
        alertDialog.show();
	}
	private class DownloadDB1 extends AsyncTask<String, String, String>
	{
		protected void onPreExecute() 
		{
			super.onPreExecute();
			ASMProgressbar1 = new ProgressDialog(getActivity());
			ASMProgressbar1.setCancelable(false);
			ASMProgressbar1.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			ASMProgressbar1.setIndeterminate(false);
			ASMProgressbar1.setMax(100);
			ASMProgressbar1.show();
			
			
		     Runnable myThread = new Runnable()
		     {
				  @Override
				  public void run() 
				  {	  
					 if (arrayLength<=100)
					 	{
					     try
						     {
						    	 progressHandler.sendMessage(progressHandler.obtainMessage());
						    	 Thread.sleep(1);
						     }
						     catch(Throwable t)
						     {
						    	
						     }
						}
				  }

				};myThread.run();
		}
		
		@Override
		protected void onProgressUpdate(String... progress) {
	    	super.onProgressUpdate(progress);
	    	String titleMsg = "Downloading  Data From -: ";
	    	ASMProgressbar1.setMessage(titleMsg+" " + progress[0] + "/" + arrayLength);
	    	 
	    }
		
	
		public int kk = 0;
		@SuppressWarnings("static-access")
		protected String doInBackground(String... params) {
			// TODO Auto-generated method stub
			try {
				String MainUrl = connectionURL
						+ "/pubuduweb/ASMWebServicesRest.svc/fSalRep/mobile123/"+DBName1+"/"+ASMName+"";

				URL json = new URL(MainUrl);
				URLConnection jc = json.openConnection();
				BufferedReader readerfdblist = new BufferedReader(
						new InputStreamReader(jc.getInputStream()));

				String line = readerfdblist.readLine();

				JSONObject jsonResponse = new JSONObject(line);

				JSONArray jsonArray = jsonResponse.getJSONArray("fSalRepResult");
				 kk = jsonArray.length();
				for (int i = 0; i < jsonArray.length(); i++) 
				{
					progressHandler.sendMessage(progressHandler.obtainMessage());
					JSONObject jObject = (JSONObject) jsonArray.get(i);
					
					//String sdbname = jObject.getString("Name");
					String RepCode = jObject.getString("RepCode");
					String RepName = jObject.getString("RepName");
					String ASECode = jObject.getString("ASECode");
									
						
						ASM_fSalRep asDblist1 = new ASM_fSalRep();
						ASM_fSalRepDS DS1 = new ASM_fSalRepDS(getActivity());
						//asDblist.setAsmD
						asDblist1.setAsmFsalrep_RepCode(RepCode.toString().trim());
						asDblist1.setAsmFsalrep_RepName(RepName.toString().trim());
						asDblist1.setAsmFsalrep_ASECode(ASECode.toString().trim());
						Log.v("ASECode", ASECode.toString());
						arrayLength = jsonArray.length()-1;
						
						publishProgress("(1/22)fSalRep "+String.valueOf(i));
						
						
						DS1.Create(asDblist1);
			
				}
					
				readerfdblist.close();
			} catch (Exception e) 
			{
				// TODO: handle exception
				Log.v("Error", e.toString());
			}
			
			try {
				String MainUrl = connectionURL
						+ "/pubuduweb/ASMWebServicesRest.svc/fASE/mobile123/"+DBName1+"/"+ASMName+"";

				URL json = new URL(MainUrl);
				URLConnection jc = json.openConnection();
				BufferedReader readerfdblist = new BufferedReader(
						new InputStreamReader(jc.getInputStream()));

				String line = readerfdblist.readLine();

				JSONObject jsonResponse = new JSONObject(line);

				JSONArray jsonArray = jsonResponse.getJSONArray("fASEResult");
				 kk = jsonArray.length();
				for (int i = 0; i < jsonArray.length(); i++) 
				{
					progressHandler.sendMessage(progressHandler.obtainMessage());
					JSONObject jObject = (JSONObject) jsonArray.get(i);
					
			
					String ASECode = jObject.getString("ASECode");
					String ASEName = jObject.getString("ASEName");
					String ASMCode = jObject.getString("ASMCode");
	
					
					Log.v("fASEResult", ASECode.toString());
					
					
						
					
						ASM_fASE asDblist = new ASM_fASE();
						ASM_fASEDS DS = new ASM_fASEDS(getActivity());
						asDblist.setAsmFAse_ASECode(ASECode.toString().trim());
						asDblist.setAsmFAse_ASEName(ASEName.toString().trim());
						asDblist.setAsmFAse_ASMCode(ASMCode.toString().trim());
						arrayLength = jsonArray.length()-1;
						
						publishProgress("(2/22)fASE "+String.valueOf(i));
						DS.Create(asDblist);
					
			
				}
					
				readerfdblist.close();
			} catch (Exception e) 
			{
				// TODO: handle exception
				Log.v("Error", e.toString());
			}
			
			try {
				String MainUrl = connectionURL
						+ "/pubuduweb/ASMWebServicesRest.svc/fBrand/mobile123/"+DBName1+"";

				URL json = new URL(MainUrl);
				URLConnection jc = json.openConnection();
				BufferedReader readerfdblist = new BufferedReader(
						new InputStreamReader(jc.getInputStream()));

				String line = readerfdblist.readLine();

				JSONObject jsonResponse = new JSONObject(line);

				JSONArray jsonArray = jsonResponse.getJSONArray("fBrandResult");
				 kk = jsonArray.length();
				for (int i = 0; i < jsonArray.length(); i++) 
				{
					progressHandler.sendMessage(progressHandler.obtainMessage());
					JSONObject jObject = (JSONObject) jsonArray.get(i);
					
			
					String BrandCode = jObject.getString("BrandCode");
					String BrandName = jObject.getString("BrandName");
					String AddUser = jObject.getString("AddUser");
					String AddDate = jObject.getString("AddDate");
					String AddMach = jObject.getString("AddMach");
					String Prefix = jObject.getString("Prefix");
					String Seqno = jObject.getString("Seqno");
	
					
					Log.v("fBrand", BrandCode.toString());
					
					
						
					
						ASM_FBrand asDblist = new ASM_FBrand();
						ASM_FBrandDS DS = new ASM_FBrandDS(getActivity());
						asDblist.setAsmFBrand_BrandCode(BrandCode.toString().trim());
						asDblist.setAsmFBrand_BrandName(BrandName.toString().trim());
						asDblist.setAsmFBrand_AddUser(AddUser.toString().trim());
						asDblist.setAsmFBrand_AddDate(AddDate.toString().trim());
						asDblist.setAsmFBrand_AddMach(AddMach.toString().trim());
						asDblist.setAsmFBrand_Prefix(Prefix.toString().trim());
						asDblist.setAsmFBrand_Seqno(Seqno.toString().trim());
						
						
						
						arrayLength = jsonArray.length()-1;
						
						publishProgress("(3/23)fBrand "+String.valueOf(i));
						DS.Create(asDblist);
					
			
				}
					
				readerfdblist.close();
			} catch (Exception e) 
			{
				// TODO: handle exception
				Log.v("Error", e.toString());
			}
			
			try {
				String MainUrl = connectionURL
						+ "/pubuduweb/ASMWebServicesRest.svc/fGroup/mobile123/"+DBName1+"";

				URL json = new URL(MainUrl);
				URLConnection jc = json.openConnection();
				BufferedReader readerfdblist = new BufferedReader(
						new InputStreamReader(jc.getInputStream()));

				String line = readerfdblist.readLine();

				JSONObject jsonResponse = new JSONObject(line);

				JSONArray jsonArray = jsonResponse.getJSONArray("fGroupResult");
				 kk = jsonArray.length();
				for (int i = 0; i < jsonArray.length(); i++) 
				{
					progressHandler.sendMessage(progressHandler.obtainMessage());
					JSONObject jObject = (JSONObject) jsonArray.get(i);
					
			
					String GroupCode = jObject.getString("GroupCode");
					String GroupName = jObject.getString("GroupName");
					
	
					
					Log.v("fGroup", GroupCode.toString());
						ASM_fGroup asDblist = new ASM_fGroup();
						ASM_fGroupDS DS = new ASM_fGroupDS(getActivity());
						asDblist.setAsmFGroup_GroupCode(GroupCode.toString().trim());
						asDblist.setAsmFGroup_GroupName(GroupName.toString().trim());
								
						arrayLength = jsonArray.length()-1;
						
						publishProgress("(4/24)fGroup "+String.valueOf(i));
						DS.Create(asDblist);
					
			
				}
					
				readerfdblist.close();
			} catch (Exception e) 
			{
				// TODO: handle exception
				Log.v("Error", e.toString());
			}
			

			ASM_fSalRepDS Repname=new ASM_fSalRepDS(getActivity());
		      
			for (int i = 0; i < Repname.getsalrep().size(); i++) 
			{
				String REpcode = Repname.getsalrep().get(i);
			
			
			try {
				String MainUrl = connectionURL
						+ "/pubuduweb/ASMWebServicesRest.svc/fRouteDet/mobile123/"+DBName1+"/"+REpcode+"";

				URL json = new URL(MainUrl);
				URLConnection jc = json.openConnection();
				BufferedReader readerfdblist = new BufferedReader(
						new InputStreamReader(jc.getInputStream()));

				String line = readerfdblist.readLine();

				JSONObject jsonResponse = new JSONObject(line);

				JSONArray jsonArray = jsonResponse.getJSONArray("fRouteDetResult");
				 kk = jsonArray.length();
				for (int i1 = 0; i1 < jsonArray.length(); i1++) 
				{
					progressHandler.sendMessage(progressHandler.obtainMessage());
					JSONObject jObject = (JSONObject) jsonArray.get(i1);
					
			
					String RouteCode = jObject.getString("RouteCode");
					String DebCode = jObject.getString("DebCode");
					
	
					
					Log.v("RouteCode", RouteCode.toString());
						ASM_fRouteDet asDblist = new ASM_fRouteDet();
						ASM_fRouteDetDS DS = new ASM_fRouteDetDS(getActivity());
						asDblist.setAsmFRoutedet_RouteCode(RouteCode.toString().trim());
						asDblist.setAsmFRoutedet_DebCode(DebCode.toString().trim());
								
						arrayLength = jsonArray.length()-1;
						
						publishProgress("(4/24)fRouteDet "+String.valueOf(i1));
						DS.Create(asDblist);
					
			
				}
					
				readerfdblist.close();
			} catch (Exception e) 
			{
				// TODO: handle exception
				Log.v("Error", e.toString());
			}
			}
			
			//ASM_fSalRepDS Repname=new ASM_fSalRepDS(getActivity());
		      
			for (int i = 0; i < Repname.getsalrep().size(); i++) 
			{
				String REpcode = Repname.getsalrep().get(i);
			
	

			try {
				String MainUrl = connectionURL
						+ "/pubuduweb/ASMWebServicesRest.svc/fordHed/mobile123/"+DBName1+"/"+REpcode+"";

				URL json = new URL(MainUrl);
				URLConnection jc = json.openConnection();
				BufferedReader readerfdblist = new BufferedReader(
						new InputStreamReader(jc.getInputStream()));

				String line = readerfdblist.readLine();

				JSONObject jsonResponse = new JSONObject(line);

				JSONArray jsonArray = jsonResponse.getJSONArray("FOrdHedResult");
				
				for (int i1 = 0; i1 < jsonArray.length(); i1++) 
				{
					progressHandler.sendMessage(progressHandler.obtainMessage());
					JSONObject jObject = (JSONObject) jsonArray.get(i1);
					
					
					String RefNo = jObject.getString("RefNo");
					String TxnDate = jObject.getString("TxnDate");
					String ManuRef = jObject.getString("ManuRef");
					String DelvDate = jObject.getString("DelvDate");
					String DealCode = jObject.getString("DealCode");
					String RepCode = jObject.getString("RepCode");
					String AreaCode = jObject.getString("AreaCode");
					String RouteCode = jObject.getString("RouteCode");
					String DebCode = jObject.getString("DebCode");
					String LocCode = jObject.getString("LocCode");
					String Remarks = jObject.getString("Remarks");
					String CostCode = jObject.getString("CostCode");
					String CurCode = jObject.getString("CurCode");
					String CurRate = jObject.getString("CurRate");
					String TxnType = jObject.getString("TxnType");
					String InvAmt = jObject.getString("InvAmt");
					String OutDis = jObject.getString("OutDis");
					String InvDis = jObject.getString("InvDis");
					String TotMkrAmt = jObject.getString("TotMkrAmt");
					String UnOvAmt = jObject.getString("UnOvAmt");
					String TotalDis = jObject.getString("TotalDis");
					String TotalItmDis = jObject.getString("TotalItmDis");
					String TotalAmt = jObject.getString("TotalAmt");
					String AddUser = jObject.getString("AddUser");
					String AddDate = jObject.getString("AddDate");
					String AddMach = jObject.getString("AddMach");
					String InvFlg = jObject.getString("InvFlg");
					String prtcopy = jObject.getString("prtcopy");
					String RecordId = jObject.getString("RecordId");
					String TranBatch = jObject.getString("TranBatch");
					String OutDisVal = jObject.getString("OutDisVal");
					String Longitude = jObject.getString("Longitude");
					String Latitude = jObject.getString("Latitude");
					String startTimeSO = jObject.getString("startTimeSO");
					String endTimeSO = jObject.getString("endTimeSO");
					String address = jObject.getString("address");
					String clssts = jObject.getString("clssts");
					String refno2 = jObject.getString("refno2");
					String PayType = jObject.getString("PayType");
					String Skutype = jObject.getString("Skutype");
					String DiffDate = jObject.getString("DiffDate");
					
					Log.v("FordHed", RefNo.toString());
					
					
						
				
						ASM_FOrdHed asDblist = new ASM_FOrdHed();
						ASM_FOrdHedDS DS = new ASM_FOrdHedDS(getActivity());
						asDblist.setAsmHED_RefNo(RefNo.toString().trim());
						asDblist.setAsmHED_TxnDate(TxnDate.toString().trim());
						asDblist.setAsmHED_ManuRef(ManuRef.toString().trim());
						asDblist.setAsmHED_DelvDate(DelvDate.toString().trim());
						asDblist.setAsmHED_DealCode(DealCode.toString().trim());
						asDblist.setAsmHED_RepCode(RepCode.toString().trim());
						asDblist.setAsmHED_AreaCode(AreaCode.toString().trim());
						asDblist.setAsmHED_RouteCode(RouteCode.toString().trim());
						asDblist.setAsmHED_DebCode(DebCode.toString().trim());
						asDblist.setAsmHED_LocCode(LocCode.toString().trim());
						asDblist.setAsmHED_Remarks(Remarks.toString().trim());
						asDblist.setAsmHED_CostCode(CostCode.toString().trim());
						asDblist.setAsmHED_CurCode(CurCode.toString().trim());
						asDblist.setAsmHED_CurRate(CurRate.toString().trim());
						asDblist.setAsmHED_TxnType(TxnType.toString().trim());
						asDblist.setAsmHED_InvAmt(InvAmt.toString().trim());
						asDblist.setAsmHED_OutDis(OutDis.toString().trim());
						asDblist.setAsmHED_InvDis(InvDis.toString().trim());
						asDblist.setAsmHED_TotMkrAmt(TotMkrAmt.toString().trim());
						asDblist.setAsmHED_UnOvAmt(UnOvAmt.toString().trim());
						asDblist.setAsmHED_TotalDis(TotalDis.toString().trim());
						asDblist.setAsmHED_TotalItmDis(TotalItmDis.toString().trim());
						asDblist.setAsmHED_TotalAmt(TotalAmt.toString().trim());
						asDblist.setAsmHED_AddUser(AddUser.toString().trim());
						asDblist.setAsmHED_AddDate(AddDate.toString().trim());
						asDblist.setAsmHED_AddMach(AddMach.toString().trim());
						asDblist.setAsmHED_InvFlg(InvFlg.toString().trim());
						asDblist.setAsmHED_prtcopy(prtcopy.toString().trim());
						asDblist.setAsmHED_RecordId(RecordId.toString().trim());
						asDblist.setAsmHED_TranBatch(TranBatch.toString().trim());
						asDblist.setAsmHED_OutDisVal(OutDisVal.toString().trim());
						asDblist.setAsmHED_Longitude(Longitude.toString().trim());
						asDblist.setAsmHED_Latitude(Latitude.toString().trim());
						asDblist.setAsmHED_startTimeSO(startTimeSO.toString().trim());
						asDblist.setAsmHED_endTimeSO(endTimeSO.toString().trim());
						asDblist.setAsmHED_address(address.toString().trim());
						asDblist.setAsmHED_clssts(clssts.toString().trim());
						asDblist.setAsmHED_refno2(refno2.toString().trim());
						asDblist.setAsmHED_PayType(PayType.toString().trim());
						asDblist.setAsmHED_Skutype(Skutype.toString().trim());
						asDblist.setAsmHED_DiffDate(DiffDate.toString().trim());
						
						DS.Create(asDblist);
						arrayLength = jsonArray.length()-1;
						
						publishProgress("(5/24)fordHed "+String.valueOf(i1));
				}
					
				readerfdblist.close();
			} catch (Exception e) 
			{
				// TODO: handle exception
				Log.v("Error", e.toString());
			}
		      
					}
					 
			
			try {
				String MainUrl = connectionURL
						+ "/pubuduweb/ASMWebServicesRest.svc/fCompanyBranch/mobile123/"+DBName1+"";

				URL json = new URL(MainUrl);
				URLConnection jc = json.openConnection();
				BufferedReader readerfdblist = new BufferedReader(
						new InputStreamReader(jc.getInputStream()));

				String line = readerfdblist.readLine();

				JSONObject jsonResponse = new JSONObject(line);

				JSONArray jsonArray = jsonResponse.getJSONArray("FCompanyBranchResult");
				 kk = jsonArray.length();
				for (int i = 0; i < jsonArray.length(); i++) 
				{
					progressHandler.sendMessage(progressHandler.obtainMessage());
					JSONObject jObject = (JSONObject) jsonArray.get(i);
					
					//String sdbname = jObject.getString("Name");
					String cSettingsCode = jObject.getString("cSettingsCode");
					String BranchCode = jObject.getString("BranchCode");
					String nNumVal = jObject.getString("nNumVal");
					String RecordId = jObject.getString("RecordId");
					
				
					
					
			
									
					arrayLength = jsonArray.length()-1;
					
					publishProgress("(6/24)fCompanyBranch "+String.valueOf(i));
						ASM_FCompanyBranch asDblist1 = new ASM_FCompanyBranch();
						ASM_FCompanyBranchDS DS1 = new ASM_FCompanyBranchDS(getActivity());
						//asDblist.setAsmD
					
						asDblist1.setAsmFCompanyBranch_cSettingsCode(cSettingsCode.toString().trim());
						asDblist1.setAsmFCompanyBranch_BranchCode(BranchCode.toString().trim());
						asDblist1.setAsmFCompanyBranch_nNumVal(nNumVal.toString().trim());
						asDblist1.setAsmFCompanyBranch_RecordId(RecordId.toString().trim());
	
				
			
						
			
						Log.v("FCompanyBranchResult", cSettingsCode.toString());
						DS1.Create(asDblist1);
			
				}
					
				readerfdblist.close();
			} catch (Exception e) 
			{
				// TODO: handle exception
				Log.v("Error", e.toString());
			}
			
			//ASM_fSalRepDS Repname=new ASM_fSalRepDS(getActivity());
		      
			for (int i = 0; i < Repname.getsalrep().size(); i++) 
			{
				String REpcode = Repname.getsalrep().get(i);
			
	
			
			try {
				String MainUrl = connectionURL
						+ "/pubuduweb/ASMWebServicesRest.svc/Forddet/mobile123/"+DBName1+"/"+REpcode+"";

				URL json = new URL(MainUrl);
				URLConnection jc = json.openConnection();
				BufferedReader readerfdblist = new BufferedReader(new InputStreamReader(jc.getInputStream()));

				String line = readerfdblist.readLine();

				JSONObject jsonResponse = new JSONObject(line);

				JSONArray jsonArray = jsonResponse.getJSONArray("FOrddetResult");
				 kk = jsonArray.length();
				for (int i1 = 0; i1 < jsonArray.length(); i1++) 
				{
					progressHandler.sendMessage(progressHandler.obtainMessage());
					JSONObject jObject = (JSONObject) jsonArray.get(i1);
					
					//String sdbname = jObject.getString("Name");
					String RefNo = jObject.getString("RefNo");
					String TxnDate = jObject.getString("TxnDate");
					String TxnType = jObject.getString("TxnType");
					String SeqNo = jObject.getString("SeqNo");
					String Itemcode = jObject.getString("Itemcode");
					String Txnseqno = jObject.getString("Txnseqno");
					String SellPrice = jObject.getString("SellPrice");
					String BSellPrice = jObject.getString("BSellPrice");
					String TSellPrice = jObject.getString("TSellPrice");
					String BTSellPrice = jObject.getString("BTSellPrice");
					String CostPrice = jObject.getString("CostPrice");
					String CaseQty = jObject.getString("CaseQty");
					String Type = jObject.getString("Type");
					String PiceQty = jObject.getString("PiceQty");
					String Qty = jObject.getString("Qty");
					String BalQty = jObject.getString("BalQty");
					String DisPer = jObject.getString("DisPer");
					String DisAmt = jObject.getString("DisAmt");
					String BDisAmt = jObject.getString("BDisAmt");
					String TaxAmt = jObject.getString("TaxAmt");
					String BTaxAmt = jObject.getString("BTaxAmt");
					String Amt = jObject.getString("Amt");
					String BAmt = jObject.getString("BAmt");
					String ReaCode = jObject.getString("ReaCode");
					String RecordId = jObject.getString("RecordId");
					String DisPerAmt = jObject.getString("DisPerAmt");
					String DisValAmt = jObject.getString("DisValAmt");
					String FrIss = jObject.getString("FrIss");
		
					Log.v("FordDet", RefNo.toString());
					
					
					
						
					arrayLength = jsonArray.length()-1;
					
					publishProgress("(7/24)Forddet "+String.valueOf(i1));
						ASM_FOrdDet asDblist1 = new ASM_FOrdDet();
						ASM_FOrdDetDS DS1 = new ASM_FOrdDetDS(getActivity());
						//asDblist.setAsmD
						asDblist1.setAsmDET_RefNo(RefNo.toString().trim());
						asDblist1.setAsmDET_TxnDate(TxnDate.toString().trim());
						asDblist1.setAsmDET_TxnType(TxnType.toString().trim());
						asDblist1.setAsmDET_SeqNo(SeqNo.toString().trim());
						asDblist1.setAsmDET_Itemcode(Itemcode.toString().trim());
						asDblist1.setAsmDET_Txnseqno(Txnseqno.toString().trim());
						asDblist1.setAsmDET_SellPrice(SellPrice.toString().trim());
						asDblist1.setAsmDET_BSellPrice(BSellPrice.toString().trim());
						asDblist1.setAsmDET_TSellPrice(TSellPrice.toString().trim());
						asDblist1.setAsmDET_BTSellPrice(BTSellPrice.toString().trim());
						asDblist1.setAsmDET_CostPrice(CostPrice.toString().trim());
						asDblist1.setAsmDET_CaseQty(CaseQty.toString().trim());
						asDblist1.setAsmDET_Type(Type.toString().trim());
						asDblist1.setAsmDET_PiceQty(PiceQty.toString().trim());
						asDblist1.setAsmDET_Qty(Qty.toString().trim());
						asDblist1.setAsmDET_BalQty(BalQty.toString().trim());
						asDblist1.setAsmDET_DisPer(DisPer.toString().trim());
						asDblist1.setAsmDET_DisAmt(DisAmt.toString().trim());
						asDblist1.setAsmDET_BDisAmt(BDisAmt.toString().trim());
						asDblist1.setAsmDET_TaxAmt(TaxAmt.toString().trim());
						asDblist1.setAsmDET_BTaxAmt(BTaxAmt.toString().trim());
						asDblist1.setAsmDET_Amt(Amt.toString().trim());
						asDblist1.setAsmDET_BAmt(BAmt.toString().trim());
						asDblist1.setAsmDET_ReaCode(ReaCode.toString().trim());
						asDblist1.setAsmDET_RecordId(RecordId.toString().trim());
						asDblist1.setAsmDET_DisPerAmt(DisPerAmt.toString().trim());
						asDblist1.setAsmDET_DisValAmt(DisValAmt.toString().trim());
						asDblist1.setAsmDET_FrIss(FrIss.toString().trim());
			
						
						DS1.Create(asDblist1);
			
				}
					
				readerfdblist.close();
			} catch (Exception e) 
			{
				// TODO: handle exception
				Log.v("Error", e.toString());
			}
			
			}
			
			
			//Fitems
			

			try {
				String MainUrl = connectionURL
						+ "/pubuduweb/ASMWebServicesRest.svc/Fitems/mobile123/"+DBName1+"";

				URL json = new URL(MainUrl);
				URLConnection jc = json.openConnection();
				BufferedReader readerfdblist = new BufferedReader(
						new InputStreamReader(jc.getInputStream()));

				String line = readerfdblist.readLine();

				JSONObject jsonResponse = new JSONObject(line);

				JSONArray jsonArray = jsonResponse.getJSONArray("fItemsResult");
				 kk = jsonArray.length();
				for (int i = 0; i < jsonArray.length(); i++) 
				{
					progressHandler.sendMessage(progressHandler.obtainMessage());
					JSONObject jObject = (JSONObject) jsonArray.get(i);
					
					//String sdbname = jObject.getString("Name");
					String SKUCode = jObject.getString("SKUCode");
					String ItemCode = jObject.getString("ItemCode");
					String ItemName = jObject.getString("ItemName");
					String BrandCode = jObject.getString("BrandCode");
					String NOUCase = jObject.getString("NOUCase");
									
					arrayLength = jsonArray.length()-1;
					
					publishProgress("(8/24)Fitems "+String.valueOf(i));
						ASM_fItems asDblist1 = new ASM_fItems();
						ASM_fItemsDS DS1 = new ASM_fItemsDS(getActivity());
						
						ASM_AVTemp asDblist3 = new ASM_AVTemp();
						ASM_AVTempDS DS2 = new ASM_AVTempDS(getActivity());
						ArrayList<ASM_AVTemp> savdet=new ArrayList<ASM_AVTemp>();
						//asDblist.setAsmD
						asDblist1.setAsmFItems_SKUCode(SKUCode.toString().trim());
						asDblist1.setAsmFItems_ItemCode(ItemCode.toString().trim());
						asDblist1.setAsmFItems_ItemName(ItemName.toString().trim());
						asDblist1.setAsmFItems_BrandCode(BrandCode.toString().trim());
						asDblist1.setAsmFItems_NOUCase(NOUCase.toString().trim());
						
						asDblist3.setAsmavTempItemname(ItemName.toString().trim());
						asDblist3.setAsmavTempItemcode(ItemCode.toString().trim());
						asDblist3.setAsmavTempBrandcode(BrandCode.toString().trim());
						
						savdet.add(asDblist3);
				
						
						
						
						Log.v("Fitems", SKUCode.toString());
	
			
						DS2.Create(savdet);
						DS1.Create(asDblist1);
			
				}
					
				readerfdblist.close();
			} catch (Exception e) 
			{
				// TODO: handle exception
				Log.v("Error", e.toString());
			}


			try {
				String MainUrl = connectionURL
						+ "/pubuduweb/ASMWebServicesRest.svc/fRoute/mobile123/"+DBName1+"/"+ASMName+"";

				URL json = new URL(MainUrl);
				URLConnection jc = json.openConnection();
				BufferedReader readerfdblist = new BufferedReader(
						new InputStreamReader(jc.getInputStream()));

				String line = readerfdblist.readLine();

				JSONObject jsonResponse = new JSONObject(line);

				JSONArray jsonArray = jsonResponse.getJSONArray("fRouteResult");
				 kk = jsonArray.length();
				for (int i = 0; i < jsonArray.length(); i++) 
				{
					progressHandler.sendMessage(progressHandler.obtainMessage());
					JSONObject jObject = (JSONObject) jsonArray.get(i);
					
					//String sdbname = jObject.getString("Name");
					String RouteCode = jObject.getString("RouteCode");
					String RouteName = jObject.getString("RouteName");
					String RepCode = jObject.getString("RepCode");
					String AreaCode = jObject.getString("AreaCode");
					String DealCode = jObject.getString("DealCode");
									
					arrayLength = jsonArray.length()-1;
					
					publishProgress("(9/24)fRoute "+String.valueOf(i));
						ASM_FRoute asDblist1 = new ASM_FRoute();
						ASM_FRouteDS DS1 = new ASM_FRouteDS(getActivity());
						//asDblist.setAsmD
						asDblist1.setAsmFR_RouteCode(RouteCode.toString().trim());
						asDblist1.setAsmFR_RouteName(RouteName.toString().trim());
						asDblist1.setAsmFR_RepCode(RepCode.toString().trim());
						asDblist1.setAsmFR_AreaCode(AreaCode.toString().trim());
						asDblist1.setAsmFR_DealCode(DealCode.toString().trim());
						
						Log.v("Froute", RouteCode.toString());
			
						
						DS1.Create(asDblist1);
			
				}
					
				readerfdblist.close();
			} catch (Exception e) 
			{
				// TODO: handle exception
				Log.v("Error", e.toString());
			}

			

			try {
				String MainUrl = connectionURL
						+ "/pubuduweb/ASMWebServicesRest.svc/fRegion/mobile123/"+DBName1+"";

				URL json = new URL(MainUrl);
				URLConnection jc = json.openConnection();
				BufferedReader readerfdblist = new BufferedReader(
						new InputStreamReader(jc.getInputStream()));

				String line = readerfdblist.readLine();

				JSONObject jsonResponse = new JSONObject(line);

				JSONArray jsonArray = jsonResponse.getJSONArray("fRegionResult");
				 kk = jsonArray.length();
				for (int i = 0; i < jsonArray.length(); i++) 
				{
					progressHandler.sendMessage(progressHandler.obtainMessage());
					JSONObject jObject = (JSONObject) jsonArray.get(i);
					
					//String sdbname = jObject.getString("Name");
					String RegCode = jObject.getString("RegCode");
					String RegName = jObject.getString("RegName");
					String ZoneCode = jObject.getString("ZoneCode");
									
					arrayLength = jsonArray.length()-1;
					
					publishProgress("(10/24)fRegion "+String.valueOf(i));
						ASM_fRegion asDblist1 = new ASM_fRegion();
						ASM_fRegionDS DS1 = new ASM_fRegionDS(getActivity());
						//asDblist.setAsmD
						asDblist1.setAsmFregion_RegCode(RegCode.toString().trim());
						asDblist1.setAsmFregion_RegName(RegName.toString().trim());
						asDblist1.setAsmFregion_ZoneCode(ZoneCode.toString().trim());
						Log.v("fRegion", RegCode.toString());
	
			
						
						DS1.Create(asDblist1);
			
				}
					
				readerfdblist.close();
			} catch (Exception e) 
			{
				// TODO: handle exception
				Log.v("Error", e.toString());
			}
			
			
//FProvince
			

			try {
				String MainUrl = connectionURL
						+ "/pubuduweb/ASMWebServicesRest.svc/fProvince/mobile123/"+DBName1+"";

				URL json = new URL(MainUrl);
				URLConnection jc = json.openConnection();
				BufferedReader readerfdblist = new BufferedReader(
						new InputStreamReader(jc.getInputStream()));

				String line = readerfdblist.readLine();

				JSONObject jsonResponse = new JSONObject(line);

				JSONArray jsonArray = jsonResponse.getJSONArray("fProvinceResult");
				 kk = jsonArray.length();
				for (int i = 0; i < jsonArray.length(); i++) 
				{
					progressHandler.sendMessage(progressHandler.obtainMessage());
					JSONObject jObject = (JSONObject) jsonArray.get(i);
					
					//String sdbname = jObject.getString("Name");
					String ProvCode = jObject.getString("ProvCode");
					String ProvName = jObject.getString("ProvName");
					String CntryCode = jObject.getString("CntryCode");
					String AddUser = jObject.getString("AddUser");
					String AddMach = jObject.getString("AddMach");
									
						
						ASM_fProvince asDblist1 = new ASM_fProvince();
						ASM_fProvinceDS DS1 = new ASM_fProvinceDS(getActivity());
						//asDblist.setAsmD
						asDblist1.setAsmFProvince_ProvCode(ProvCode.toString().trim());
						asDblist1.setAsmFProvince_ProvName(ProvName.toString().trim());
						asDblist1.setAsmFProvince_CntryCode(CntryCode.toString().trim());
						asDblist1.setAsmFProvince_AddUser(AddUser.toString().trim());
						asDblist1.setAsmFProvince_AddMach(AddMach.toString().trim());
						
						Log.v("fRegion", ProvCode.toString());
						arrayLength = jsonArray.length()-1;
						
						publishProgress("(11/25)fProvince "+String.valueOf(i));
			
						
						DS1.Create(asDblist1);
			
				}
					
				readerfdblist.close();
			} catch (Exception e) 
			{
				// TODO: handle exception
				Log.v("Error", e.toString());
			}
			
			
//Flocation
			//Url data not working

			try {
				String MainUrl = connectionURL
						+ "/pubuduweb/ASMWebServicesRest.svc/FLocation/mobile123/"+DBName1+"";

				URL json = new URL(MainUrl);
				URLConnection jc = json.openConnection();
				BufferedReader readerfdblist = new BufferedReader(
						new InputStreamReader(jc.getInputStream()));

				String line = readerfdblist.readLine();

				JSONObject jsonResponse = new JSONObject(line);

				JSONArray jsonArray = jsonResponse.getJSONArray("fLocationResult");
				 kk = jsonArray.length();
				for (int i = 0; i < jsonArray.length(); i++) 
				{
					progressHandler.sendMessage(progressHandler.obtainMessage());
					JSONObject jObject = (JSONObject) jsonArray.get(i);
					
					//String sdbname = jObject.getString("Name");
					String LocCode = jObject.getString("LocCode");
					String LocName = jObject.getString("LocName");
					String TeleNo = jObject.getString("TeleNo");
					String FaxNo = jObject.getString("FaxNo");
					String EMail = jObject.getString("EMail");
					
					String LoctCode = jObject.getString("LoctCode");
					String RepCode = jObject.getString("RepCode");
					String Contact = jObject.getString("Contact");
					String Address1 = jObject.getString("Address1");
					String Address2 = jObject.getString("Address2");
					
					String Address3 = jObject.getString("Address3");
					String AddUser = jObject.getString("AddUser");
					String AddMach = jObject.getString("AddMach");
									
						
						ASM_FLocation asDblist1 = new ASM_FLocation();
						ASM_FLocationDS DS1 = new ASM_FLocationDS(getActivity());
						//asDblist.setAsmD
						asDblist1.setAsmLoc_LocCode(LocCode.toString().trim());
						asDblist1.setAsmLoc_LocName(LocName.toString().trim());
						asDblist1.setAsmLoc_TeleNo(TeleNo.toString().trim());
						asDblist1.setAsmLoc_FaxNo(FaxNo.toString().trim());
						asDblist1.setAsmLoc_EMail(EMail.toString().trim());
						asDblist1.setAsmLoc_LoctCode(LoctCode.toString().trim());
						asDblist1.setAsmLoc_RepCode(RepCode.toString().trim());
						asDblist1.setAsmLoc_Contact(Contact.toString().trim());
						asDblist1.setAsmLoc_Address1(Address1.toString().trim());
						asDblist1.setAsmLoc_Address2(Address2.toString().trim());
						asDblist1.setAsmLoc_Address3(Address3.toString().trim());
						asDblist1.setAsmLoc_AddUser(AddUser.toString().trim());
						asDblist1.setAsmLoc_AddMach(AddMach.toString().trim());
						Log.v("FLocation", LocCode.toString());
						arrayLength = jsonArray.length()-1;
						
						publishProgress("(12/24)FLocation "+String.valueOf(i));
			
						
						DS1.Create(asDblist1);
			
				}
					
				readerfdblist.close();
			} catch (Exception e) 
			{
				// TODO: handle exception
				Log.v("Error", e.toString());
			}
			
//FItenrDet  //have to pass the refno code????
			

			try {
				String MainUrl = connectionURL
						+ "/pubuduweb/ASMWebServicesRest.svc/FItenrDet/mobile123/"+DBName1+"/"+ASMName+"";

				URL json = new URL(MainUrl);
				URLConnection jc = json.openConnection();
				BufferedReader readerfdblist = new BufferedReader(
						new InputStreamReader(jc.getInputStream()));

				String line = readerfdblist.readLine();

				JSONObject jsonResponse = new JSONObject(line);

				JSONArray jsonArray = jsonResponse.getJSONArray("FItenrDetResult");
				 kk = jsonArray.length();
				for (int i = 0; i < jsonArray.length(); i++) 
				{
					progressHandler.sendMessage(progressHandler.obtainMessage());
					JSONObject jObject = (JSONObject) jsonArray.get(i);
					
					//String sdbname = jObject.getString("Name");
					String RefNo = jObject.getString("RefNo");
					String TxnDate = jObject.getString("TxnDate");
					String RDTarget = jObject.getString("RDTarget");
					String RouteCode = jObject.getString("RouteCode");
									
						
						ASM_FItenrDet asDblist1 = new ASM_FItenrDet();
						ASM_FItenrDetDS DS1 = new ASM_FItenrDetDS(getActivity());
						//asDblist.setAsmD
						asDblist1.setAsmFItenrdet_RefNo(RefNo.toString().trim());
						asDblist1.setAsmFItenrdet_TxnDate(TxnDate.toString().trim());
						asDblist1.setAsmFItenrdet_RDTarget(RDTarget.toString().trim());
						asDblist1.setAsmFItenrdet_RouteCode(RouteCode.toString().trim());
						
						Log.v("FItenrDet", RefNo.toString());
			
						arrayLength = jsonArray.length()-1;
						
						publishProgress("(13/24)FItenrDet "+String.valueOf(i));
						DS1.Create(asDblist1);
			
				}
					
				readerfdblist.close();
			} catch (Exception e) 
			{
				// TODO: handle exception
				Log.v("Error", e.toString());
			}
			
//FExpense
			

			try {
				String MainUrl = connectionURL
						+ "/pubuduweb/ASMWebServicesRest.svc/fExpense/mobile123/"+DBName1+"";

				URL json = new URL(MainUrl);
				URLConnection jc = json.openConnection();
				BufferedReader readerfdblist = new BufferedReader(
						new InputStreamReader(jc.getInputStream()));

				String line = readerfdblist.readLine();

				JSONObject jsonResponse = new JSONObject(line);

				JSONArray jsonArray = jsonResponse.getJSONArray("fExpenseResult");
				 kk = jsonArray.length();
				for (int i = 0; i < jsonArray.length(); i++) 
				{
					progressHandler.sendMessage(progressHandler.obtainMessage());
					JSONObject jObject = (JSONObject) jsonArray.get(i);
					
					//String sdbname = jObject.getString("Name");
					String ExpCode = jObject.getString("ExpCode");
					String ExpName = jObject.getString("ExpName");
					String ExpGrpCode = jObject.getString("ExpGrpCode");
					String Status = jObject.getString("Status");
					String AddUser = jObject.getString("AddUser");
					String AddDate = jObject.getString("AddDate");
					String AddMach = jObject.getString("AddMach");
					String RecordId = jObject.getString("RecordId");
									
						
						ASM_fExpense asDblist1 = new ASM_fExpense();
						ASM_fExpenseDS DS1 = new ASM_fExpenseDS(getActivity());
						//asDblist.setAsmD
						asDblist1.setAsmFexpen_ExpCode(ExpCode.toString().trim());
						asDblist1.setAsmFexpen_ExpName(ExpName.toString().trim());
						asDblist1.setAsmFexpen_ExpGrpCode(ExpGrpCode.toString().trim());
						asDblist1.setAsmFexpen_Status(Status.toString().trim());
						asDblist1.setAsmFexpen_AddUser(AddUser.toString().trim());
						asDblist1.setAsmFexpen_AddDate(AddDate.toString().trim());
						asDblist1.setAsmFexpen_AddMach(AddMach.toString().trim());
						asDblist1.setAsmFexpen_RecordId(RecordId.toString().trim());
						
						Log.v("fExpense", ExpCode.toString());
						arrayLength = jsonArray.length()-1;
						
						publishProgress("(14/24)fExpense "+String.valueOf(i));
						
						DS1.Create(asDblist1);
			
				}
					
				readerfdblist.close();
			} catch (Exception e) 
			{
				// TODO: handle exception
				Log.v("Error", e.toString());
			
			}
			
			
//FInvfreeIss
			

			try {
				String MainUrl = connectionURL
						+ "/pubuduweb/ASMWebServicesRest.svc/fInvFreeIss/mobile123/"+DBName1+"";

				URL json = new URL(MainUrl);
				URLConnection jc = json.openConnection();
				BufferedReader readerfdblist = new BufferedReader(
						new InputStreamReader(jc.getInputStream()));

				String line = readerfdblist.readLine();

				JSONObject jsonResponse = new JSONObject(line);

				JSONArray jsonArray = jsonResponse.getJSONArray("fInvFreeIssResult");
				 kk = jsonArray.length();
				for (int i = 0; i < jsonArray.length(); i++) 
				{
					progressHandler.sendMessage(progressHandler.obtainMessage());
					JSONObject jObject = (JSONObject) jsonArray.get(i);
					
					//String sdbname = jObject.getString("Name");
					String RefNo = jObject.getString("RefNo");
					String TxnDate = jObject.getString("TxnDate");
					String RefNo1 = jObject.getString("RefNo1");
					String Itemcode = jObject.getString("Itemcode");
					String Qty = jObject.getString("Qty");
					String RecordId = jObject.getString("RecordId");
									
						
						ASM_FinvFreeIss asDblist1 = new ASM_FinvFreeIss();
						ASM_FinvFreeIssDS DS1 = new ASM_FinvFreeIssDS(getActivity());
						//asDblist.setAsmD
						asDblist1.setAsmFreeIss_RefNo(RefNo.toString().trim());
						asDblist1.setAsmFreeIss_TxnDate(TxnDate.toString().trim());
						asDblist1.setAsmFreeIss_RefNo1(RefNo1.toString().trim());
						asDblist1.setAsmFreeIss_itemcode(Itemcode.toString().trim());
						asDblist1.setAsmFreeIss_Qty(Qty.toString().trim());
						asDblist1.setAsmFreeIss_RecordId(RecordId.toString().trim());
						Log.v("fInvFreeIss", RefNo.toString());
						arrayLength = jsonArray.length()-1;
						
						publishProgress("(15/25)fInvFreeIss "+String.valueOf(i));
						DS1.Create(asDblist1);
			
				}
					
				readerfdblist.close();
			} catch (Exception e) 
			{
				// TODO: handle exception
				Log.v("Error", e.toString());
			
			}
			
			
//fInvrdet
			

			try {
				String MainUrl = connectionURL
						+ "/pubuduweb/ASMWebServicesRest.svc/fInvrdet/mobile123/"+DBName1+"/"+ASMName+"";

				URL json = new URL(MainUrl);
				URLConnection jc = json.openConnection();
				BufferedReader readerfdblist = new BufferedReader(
						new InputStreamReader(jc.getInputStream()));

				String line = readerfdblist.readLine();

				JSONObject jsonResponse = new JSONObject(line);

				JSONArray jsonArray = jsonResponse.getJSONArray("fInvrdetResult");
				 kk = jsonArray.length();
				for (int i = 0; i < jsonArray.length(); i++) 
				{
					progressHandler.sendMessage(progressHandler.obtainMessage());
					JSONObject jObject = (JSONObject) jsonArray.get(i);
					
					//String sdbname = jObject.getString("Name");
					String RefNo = jObject.getString("RefNo");
					String TxnDate = jObject.getString("TxnDate");
					String TxnType = jObject.getString("TxnType");
					String ItemCode = jObject.getString("ItemCode");
					String BAmt = jObject.getString("BAmt");
					String Type = jObject.getString("Type");
					String CaseQty = jObject.getString("CaseQty");
					String PiceQty = jObject.getString("PiceQty");
									
						
						ASM_fInvrdet asDblist1 = new ASM_fInvrdet();
						ASM_fInvrdetDS DS1 = new ASM_fInvrdetDS(getActivity());
						//asDblist.setAsmD
						asDblist1.setAsmInvrdet_RefNo(RefNo.toString().trim());
						asDblist1.setAsmInvrdet_TxnDate(TxnDate.toString().trim());
						asDblist1.setAsmInvrdet_TxnType(TxnType.toString().trim());
						asDblist1.setAsmInvrdet_ItemCode(ItemCode.toString().trim());
						asDblist1.setAsmInvrdet_BAmt(BAmt.toString().trim());
						asDblist1.setAsmInvrdet_Type(Type.toString().trim());
						asDblist1.setAsmInvrdet_CaseQty(CaseQty.toString().trim());
						asDblist1.setAsmInvrdet_PiceQty(PiceQty.toString().trim());
						Log.v("fInvrdet", RefNo.toString());
						arrayLength = jsonArray.length()-1;
						
						publishProgress("(16/24)fInvrdet "+String.valueOf(i));
						DS1.Create(asDblist1);
			
				}
					
				readerfdblist.close();
			} catch (Exception e) 
			{
				// TODO: handle exception
				Log.v("Error", e.toString());
			
			}
			
			
//fInvrhed
			

			try {
				String MainUrl = connectionURL
						+ "/pubuduweb/ASMWebServicesRest.svc/fInvrhed/mobile123/"+DBName1+"/"+ASMName+"";

				URL json = new URL(MainUrl);
				URLConnection jc = json.openConnection();
				BufferedReader readerfdblist = new BufferedReader(
						new InputStreamReader(jc.getInputStream()));

				String line = readerfdblist.readLine();

				JSONObject jsonResponse = new JSONObject(line);

				JSONArray jsonArray = jsonResponse.getJSONArray("fInvrhedResult");
				 kk = jsonArray.length();
				for (int i = 0; i < jsonArray.length(); i++) 
				{
					progressHandler.sendMessage(progressHandler.obtainMessage());
					JSONObject jObject = (JSONObject) jsonArray.get(i);
					
					//String sdbname = jObject.getString("Name");
					String RefNo = jObject.getString("RefNo");
					String TxnDate = jObject.getString("TxnDate");
					String RefNo1 = jObject.getString("RefNo1");
					String TxnType = jObject.getString("TxnType");
					String RepCode = jObject.getString("RepCode");
					String BTotalAmt = jObject.getString("BTotalAmt");
					String DebCode = jObject.getString("DebCode");
					String AreaCode = jObject.getString("AreaCode");
					String Invflg = jObject.getString("Invflg");
					String Remarks = jObject.getString("Remarks");
					String LocCode = jObject.getString("LocCode");
									
						
						ASM_fInvrhed asDblist1 = new ASM_fInvrhed();
						ASM_fInvrhedDS DS1 = new ASM_fInvrhedDS(getActivity());
						//asDblist.setAsmD
						asDblist1.setAsmInvrhed_RefNo(RefNo.toString().trim());
						asDblist1.setAsmInvrhed_TxnDate(TxnDate.toString().trim());
						asDblist1.setAsmInvrhed_RefNo1(RefNo1.toString().trim());
						asDblist1.setAsmInvrhed_TxnType(TxnType.toString().trim());
						asDblist1.setAsmInvrhed_RepCode(RepCode.toString().trim());
						asDblist1.setAsmInvrhed_BTotalAmt(BTotalAmt.toString().trim());
						asDblist1.setAsmInvrhed_DebCode(DebCode.toString().trim());
						asDblist1.setAsmInvrhed_AreaCode(AreaCode.toString().trim());
						asDblist1.setAsmInvrhed_Invflg(Invflg.toString().trim());
						asDblist1.setAsmInvrhed_Remarks(Remarks.toString().trim());
						asDblist1.setAsmInvrhed_LocCode(LocCode.toString().trim());
						Log.v("fInvrhed", RefNo.toString());
						arrayLength = jsonArray.length()-1;
						
						publishProgress("(17/24)fInvrhed "+String.valueOf(i));
						DS1.Create(asDblist1);
			
				}
					
				readerfdblist.close();
			} catch (Exception e) 
			{
				// TODO: handle exception
				Log.v("Error", e.toString());
			
			}
	
//FArea
			

			try {
				String MainUrl = connectionURL
						+ "/pubuduweb/ASMWebServicesRest.svc/fArea/mobile123/"+DBName1+"/"+ASMName+"";

				URL json = new URL(MainUrl);
				URLConnection jc = json.openConnection();
				BufferedReader readerfdblist = new BufferedReader(
						new InputStreamReader(jc.getInputStream()));

				String line = readerfdblist.readLine();

				JSONObject jsonResponse = new JSONObject(line);

				JSONArray jsonArray = jsonResponse.getJSONArray("fAreaResult");
				 kk = jsonArray.length();
				for (int i = 0; i < jsonArray.length(); i++) 
				{
					progressHandler.sendMessage(progressHandler.obtainMessage());
					JSONObject jObject = (JSONObject) jsonArray.get(i);
					
					//String sdbname = jObject.getString("Name");
					String AreaCode = jObject.getString("AreaCode");
					String AreaName = jObject.getString("AreaName");
					String RegCode = jObject.getString("RegCode");
					String DealCode = jObject.getString("DealCode");
									
						
						ASM_FArea asDblist1 = new ASM_FArea();
						ASM_FAreaDS DS1 = new ASM_FAreaDS(getActivity());
						//asDblist.setAsmD
						asDblist1.setAsmAr_AreaCode(AreaCode.toString().trim());
						asDblist1.setAsmAr_AreaName(AreaName.toString().trim());
						asDblist1.setAsmAr_RegCode(RegCode.toString().trim());
						asDblist1.setAsmAr_DealCode(DealCode.toString().trim());
						Log.v("fArea", AreaCode.toString());
						arrayLength = jsonArray.length()-1;
						
						publishProgress("(18/24)fArea "+String.valueOf(i));
						
						DS1.Create(asDblist1);
			
				}
					
				readerfdblist.close();
			} catch (Exception e) 
			{
				// TODO: handle exception
				Log.v("Error", e.toString());
			}
			for (int i = 0; i < Repname.getsalrep().size(); i++) 
			{
				String REpcode = Repname.getsalrep().get(i);
			
			
			try {
				String MainUrl = connectionURL
						+ "/pubuduweb/ASMWebServicesRest.svc/Fdebtor/mobile123/"+DBName1+"/"+REpcode+"";

				URL json = new URL(MainUrl);
				URLConnection jc = json.openConnection();
				BufferedReader readerfdblist = new BufferedReader(
						new InputStreamReader(jc.getInputStream()));

				String line = readerfdblist.readLine();

				JSONObject jsonResponse = new JSONObject(line);

				JSONArray jsonArray = jsonResponse.getJSONArray("FdebtorResult");
				 kk = jsonArray.length();
				for (int i1 = 0; i1 < jsonArray.length(); i1++) 
				{
					progressHandler.sendMessage(progressHandler.obtainMessage());
					JSONObject jObject = (JSONObject) jsonArray.get(i1);
					
					//String sdbname = jObject.getString("Name");
					String DebCode = jObject.getString("DebCode");
					String DebName = jObject.getString("DebName");
					String AreaCode = jObject.getString("AreaCode");
					String DealCode = jObject.getString("DealCode");
					String RepCode = jObject.getString("RepCode");
					String OutCode = jObject.getString("OutCode");
									
						
						ASM_Fdebtor asDblist1 = new ASM_Fdebtor();
						ASM_FdebtorDS DS1 = new ASM_FdebtorDS(getActivity());
						//asDblist.setAsmD
					
						asDblist1.setAsmFdebtor_DebCode(DebCode.toString().trim());
						asDblist1.setAsmFdebtor_DebName(DebName.toString().trim());
						asDblist1.setAsmFdebtor_AreaCode(AreaCode.toString().trim());
						asDblist1.setAsmFdebtor_RepCode(RepCode.toString().trim());
						asDblist1.setAsmFdebtor_DealCode(DealCode.toString().trim());
						asDblist1.setAsmFdebtor_OutCode(OutCode.toString().trim());
						Log.v("Fdebtor", DebCode.toString());
						arrayLength = jsonArray.length()-1;
						
						publishProgress("(19/24)Fdebtor "+String.valueOf(i1));
						
						DS1.Create(asDblist1);
			
				}
					
				readerfdblist.close();
			} catch (Exception e) 
			{
				// TODO: handle exception
				Log.v("Error", e.toString());
			}
			}
			
			//fINVrDET
			for (int i = 0; i < Repname.getsalrep().size(); i++) 
			{
				String REpcode = Repname.getsalrep().get(i);
			try {
				String MainUrl = connectionURL
						+ "/pubuduweb/ASMWebServicesRest.svc/FInvDet/mobile123/"+DBName1+"/"+REpcode+"";

				URL json = new URL(MainUrl);
				URLConnection jc = json.openConnection();
				BufferedReader readerfdblist = new BufferedReader(
						new InputStreamReader(jc.getInputStream()));

				String line = readerfdblist.readLine();

				JSONObject jsonResponse = new JSONObject(line);

				JSONArray jsonArray = jsonResponse.getJSONArray("FInvDetResult");
				 kk = jsonArray.length();
				for (int i1 = 0; i1 < jsonArray.length(); i1++) 
				{
					progressHandler.sendMessage(progressHandler.obtainMessage());
					JSONObject jObject = (JSONObject) jsonArray.get(i1);
					
					//String sdbname = jObject.getString("Name");
					String RefNo = jObject.getString("RefNo");
					String TxnDate = jObject.getString("TxnDate");
					String TxnType = jObject.getString("TxnType");
					String ItemCode = jObject.getString("ItemCode");
					String Type = jObject.getString("Type");
					String Bamt = jObject.getString("Bamt");
					String FrIss = jObject.getString("FrIss");
									
						
						ASM_FinvDet asDblist1 = new ASM_FinvDet();
						ASM_FinvDetDS DS1 = new ASM_FinvDetDS(getActivity());
						//asDblist.setAsmD
					
						asDblist1.setAsmInvDet_RefNo(RefNo.toString().trim());
						asDblist1.setAsmInvDet_TxnDate(TxnDate.toString().trim());
						asDblist1.setAsmInvDet_TxnType(TxnType.toString().trim());
						asDblist1.setAsmInvDet_Itemcode(ItemCode.toString().trim());
						asDblist1.setAsmInvDet_Type(Type.toString().trim());
						asDblist1.setAsmInvDet_BAmt(Bamt.toString().trim());
						asDblist1.setAsmInvDet_FrIss(FrIss.toString().trim());
					
						arrayLength = jsonArray.length()-1;
						
						publishProgress("(20/24)FInvDet"+String.valueOf(i1));
						Log.v("FInvDet", RefNo.toString());
						
						DS1.Create(asDblist1);
			
				}
					
				readerfdblist.close();
			} catch (Exception e) 
			{
				// TODO: handle exception
				Log.v("Error", e.toString());
			}
			}
			
			for (int i = 0; i < Repname.getsalrep().size(); i++) 
			{
				String REpcode = Repname.getsalrep().get(i);
			
			try {
				String MainUrl = connectionURL
						+ "/pubuduweb/ASMWebServicesRest.svc/FInvHed/mobile123/"+DBName1+"/"+REpcode+"";

				URL json = new URL(MainUrl);
				URLConnection jc = json.openConnection();
				BufferedReader readerfdblist = new BufferedReader(
						new InputStreamReader(jc.getInputStream()));

				String line = readerfdblist.readLine();

				JSONObject jsonResponse = new JSONObject(line);

				JSONArray jsonArray = jsonResponse.getJSONArray("FInvHedResult");
				 kk = jsonArray.length();
				for (int i1 = 0; i1 < jsonArray.length(); i1++) 
				{
					progressHandler.sendMessage(progressHandler.obtainMessage());
					JSONObject jObject = (JSONObject) jsonArray.get(i1);
					
					//String sdbname = jObject.getString("Name");
					String RefNo = jObject.getString("RefNo");
					String RefNo1 = jObject.getString("RefNo1");
					String TxnDate = jObject.getString("TxnDate");
					String DealCode = jObject.getString("DealCode");
					String RepCode = jObject.getString("RepCode");
					
					String AreaCode = jObject.getString("AreaCode");
					String DebCode = jObject.getString("DebCode");
					String TotalAmt = jObject.getString("TotalAmt");
					String AddDate =jObject.getString("AddDate");
									
						
						ASM_FinvHed asDblist1 = new ASM_FinvHed();
						ASM_FinvHedDS DS1 = new ASM_FinvHedDS(getActivity());
						//asDblist.setAsmD
					
						asDblist1.setAsmInvHed_RefNo(RefNo.toString().trim());
						asDblist1.setAsmInvHed_RefNo1(RefNo1.toString().trim());
						asDblist1.setAsmInvHed_TxnDate(TxnDate.toString().trim());
						asDblist1.setAsmInvHed_DealCode(DealCode.toString().trim());
						
						asDblist1.setAsmInvHed_RepCode(RepCode.toString().trim());
						
						
						asDblist1.setAsmInvHed_AreaCode(AreaCode.toString().trim());
						asDblist1.setAsmInvHed_DebCode(DebCode.toString().trim());
						asDblist1.setAsmInvHed_TotalAmt(TotalAmt.toString().trim());
						asDblist1.setAsmInvHed_AddDate(AddDate.toString().trim());
						
						Log.v("(20/23)FInvHed", RefNo.toString());
						publishProgress("(21/24)FInvHed"+String.valueOf(i1));
						arrayLength = jsonArray.length()-1;
						
						publishProgress("FInvHed "+String.valueOf(i1));
						DS1.Create(asDblist1);
			
				}
					
				readerfdblist.close();
			} catch (Exception e) 
			{
				// TODO: handle exception
				Log.v("Error", e.toString());
			}
			
			}
			
			try {
				String MainUrl = connectionURL
						+ "/pubuduweb/ASMWebServicesRest.svc/fDealer/mobile123/"+DBName1+"";

				URL json = new URL(MainUrl);
				URLConnection jc = json.openConnection();
				BufferedReader readerfdblist = new BufferedReader(
						new InputStreamReader(jc.getInputStream()));

				String line = readerfdblist.readLine();

				JSONObject jsonResponse = new JSONObject(line);

				JSONArray jsonArray = jsonResponse.getJSONArray("fDealerResult");
				 kk = jsonArray.length();
				for (int i = 0; i < jsonArray.length(); i++) 
				{
					progressHandler.sendMessage(progressHandler.obtainMessage());
					JSONObject jObject = (JSONObject) jsonArray.get(i);
					
					//String sdbname = jObject.getString("Name");
					String DealCode = jObject.getString("DealCode");
					String DealName = jObject.getString("DealName");
			
									
						
						ASM_fDealer asDblist1 = new ASM_fDealer();
						ASM_fDealerDS DS1 = new ASM_fDealerDS(getActivity());
						//asDblist.setAsmD
					
						asDblist1.setAsmFdealer_DealCode(DealCode.toString().trim());
						asDblist1.setAsmFdealer_DealName(DealName.toString().trim());
						arrayLength = jsonArray.length()-1;
						
						publishProgress("(22/24)fDealer "+String.valueOf(i));
						Log.v("fDealer", DealCode.toString());
						DS1.Create(asDblist1);
			
				}
					
				readerfdblist.close();
			} catch (Exception e) 
			{
				// TODO: handle exception
				Log.v("Error", e.toString());
			}
			
			
			try {
				String MainUrl = connectionURL
						+ "/pubuduweb/ASMWebServicesRest.svc/fReason/mobile123/"+DBName1+"";

				URL json = new URL(MainUrl);
				URLConnection jc = json.openConnection();
				BufferedReader readerfdblist = new BufferedReader(
						new InputStreamReader(jc.getInputStream()));

				String line = readerfdblist.readLine();

				JSONObject jsonResponse = new JSONObject(line);

				JSONArray jsonArray = jsonResponse.getJSONArray("fReasonResult");
				 kk = jsonArray.length();
				for (int i = 0; i < jsonArray.length(); i++) 
				{
					progressHandler.sendMessage(progressHandler.obtainMessage());
					JSONObject jObject = (JSONObject) jsonArray.get(i);
					
					//String sdbname = jObject.getString("Name");
					String ReaCode = jObject.getString("ReaCode");
					String ReaName = jObject.getString("ReaName");
					String Type = jObject.getString("Type");
					String ReaTcode = jObject.getString("ReaTcode");
					String AddUser = jObject.getString("AddUser");
					String AddDate = jObject.getString("AddDate");
					String AddMach = jObject.getString("AddMach");
					
			
									
						
						ASM_fReason asDblist1 = new ASM_fReason();
						ASM_fReasonDS DS1 = new ASM_fReasonDS(getActivity());
						//asDblist.setAsmD
					
						asDblist1.setAsmFreason_ReaCode(ReaCode.toString().trim());
						asDblist1.setAsmFreason_ReaName(ReaName.toString().trim());
						asDblist1.setAsmFreason_Type(Type.toString().trim());
						asDblist1.setAsmFreason_ReaTcode(ReaTcode.toString().trim());
						asDblist1.setAsmFreason_AddUser(AddUser.toString().trim());
						asDblist1.setAsmFreason_AddDate(AddDate.toString().trim());
						asDblist1.setAsmFreason_AddMach(AddMach.toString().trim());
						arrayLength = jsonArray.length()-1;
						
						publishProgress("(23/24)fReason "+String.valueOf(i));
			
						Log.v("FReason", ReaCode.toString());
						DS1.Create(asDblist1);
			
				}
					
				readerfdblist.close();
			} catch (Exception e) 
			{
				// TODO: handle exception
				Log.v("Error", e.toString());
			}
			
			
			
			try {
				String MainUrl = connectionURL
						+ "/pubuduweb/ASMWebServicesRest.svc/fCompanySetting/mobile123/"+DBName1+"";

				URL json = new URL(MainUrl);
				URLConnection jc = json.openConnection();
				BufferedReader readerfdblist = new BufferedReader(
						new InputStreamReader(jc.getInputStream()));

				String line = readerfdblist.readLine();

				JSONObject jsonResponse = new JSONObject(line);

				JSONArray jsonArray = jsonResponse.getJSONArray("fCompanySettingResult");
				 kk = jsonArray.length();
				for (int i = 0; i < jsonArray.length(); i++) 
				{
					progressHandler.sendMessage(progressHandler.obtainMessage());
					JSONObject jObject = (JSONObject) jsonArray.get(i);
					
					//String sdbname = jObject.getString("Name");
					String cSettingsCode = jObject.getString("cSettingsCode");
					String cSettingGrp = jObject.getString("cSettingGrp");
					String cLocationChar = jObject.getString("cLocationChar");
					String cCharVal = jObject.getString("cCharVal");
					String nNumVal = jObject.getString("nNumVal");
					String cRemarks = jObject.getString("cRemarks");
					String nType = jObject.getString("nType");
					String bUpdateFlag = jObject.getString("bUpdateFlag");
					String cCompanyCode = jObject.getString("cCompanyCode");
					String recordid = jObject.getString("recordid");
					
					
			
									
						
						ASM_fCompanySetting asDblist1 = new ASM_fCompanySetting();
						ASM_fCompanySettingDS DS1 = new ASM_fCompanySettingDS(getActivity());
						//asDblist.setAsmD
					
						asDblist1.setAsmfCompanySetting_cSettingsCode(cSettingsCode.toString().trim());
						asDblist1.setAsmfCompanySetting_cSettingGrp(cSettingGrp.toString().trim());
						asDblist1.setAsmfCompanySetting_cLocationChar(cLocationChar.toString().trim());
						asDblist1.setAsmfCompanySetting_cCharVal(cCharVal.toString().trim());
						asDblist1.setAsmfCompanySetting_nNumVal(nNumVal.toString().trim());
						asDblist1.setAsmfCompanySetting_cRemarks(cRemarks.toString().trim());
						asDblist1.setAsmfCompanySetting_nType(nType.toString().trim());
						asDblist1.setAsmfCompanySetting_bUpdateFlag(bUpdateFlag.toString().trim());
						asDblist1.setAsmfCompanySetting_cCompanyCode(cCompanyCode.toString().trim());
						asDblist1.setAsmfCompanySetting_recordid(recordid.toString().trim());
			
						
						arrayLength = jsonArray.length()-1;
						
						publishProgress("(24/24)fCompanySetting "+String.valueOf(i));
						Log.v("fCompanySettingResult", cSettingsCode.toString());
						DS1.Create(asDblist1);
			
				}
					
				readerfdblist.close();
			} catch (Exception e) 
			{
				// TODO: handle exception
				Log.v("Error", e.toString());
			}
			
			

			
			
			return null;
		}
		
		protected void onPostExecute(String args) {
			
			ASMProgressbar1.dismiss();
				Login();

				
			return;
		}
		@Override
		protected void onCancelled() 
		{
			AlertDialog.Builder builder1 = new AlertDialog.Builder(getActivity());
			builder1.setTitle("ERROR");
			builder1.setIcon(R.drawable.ic_error);
            builder1.setMessage("Download Not Complete..Try Again..");
            builder1.setCancelable(true);
            builder1.setPositiveButton("Ok",
            new DialogInterface.OnClickListener() 
            {
                public void onClick(DialogInterface dg, int id) 
                {
                	dg.dismiss();
           
                }
            });
            AlertDialog alert11 = builder1.create();
            alert11.show();
		}
	
		
		Handler progressHandler = new Handler()
		{
			public void handleMessage(Message msg) 
			{
				ASMProgressbar1.incrementProgressBy(arrayLength);
			}
		};
	
	
	}
	

	
	//getASM
	private class DownloadDB2 extends AsyncTask<Void, Void, Void>
	{
		protected void onPreExecute() 
		{
			super.onPreExecute();
			ASMProgressbar1 = new ProgressDialog(getActivity());
			ASMProgressbar1.setCancelable(false);
			ASMProgressbar1.setMessage("Get ASM List...");
			ASMProgressbar1.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			ASMProgressbar1.setIndeterminate(false);
			ASMProgressbar1.setMax(100);
			ASMProgressbar1.show();
			
			
		     Runnable myThread = new Runnable()
		     {
				  @Override
				  public void run() 
				  {	  
					 if (arrayLength<=100)
					 	{
					     try
						     {
						    	 progressHandler.sendMessage(progressHandler.obtainMessage());
						    	 Thread.sleep(1);
						     }
						     catch(Throwable t)
						     {
						    	
						     }
						}
				  }

				};myThread.run();
		}
		
		public int kk = 0;
		@SuppressWarnings("static-access")
		@Override
		protected Void doInBackground(Void... params) 
		{
			// TODO Auto-generated method stub
			try {
				String MainUrl = connectionURL
						+ "/pubuduweb/ASMWebServicesRest.svc/FASM/mobile123/"+DBName1+"";

				URL json = new URL(MainUrl);
				URLConnection jc = json.openConnection();
				BufferedReader readerfdblist = new BufferedReader(
						new InputStreamReader(jc.getInputStream()));

				String line = readerfdblist.readLine();

				JSONObject jsonResponse = new JSONObject(line);

				JSONArray jsonArray = jsonResponse.getJSONArray("FASMResult");
				 kk = jsonArray.length();
				for (int i = 0; i < jsonArray.length(); i++) 
				{
					progressHandler.sendMessage(progressHandler.obtainMessage());
					JSONObject jObject = (JSONObject) jsonArray.get(i);
					
					//String sdbname = jObject.getString("Name");
					String ASMCode = jObject.getString("ASMCode");
					String ASMName = jObject.getString("ASMName");
					String RSMCode = jObject.getString("RSMCode");
					String ASMMob = jObject.getString("ASMMob");
					String ASMTele = jObject.getString("ASMTele");
					String ASMEMail = jObject.getString("ASMEMail");
					String Status = jObject.getString("Status");
					String AddUser = jObject.getString("AddUser");
					String AddDate = jObject.getString("AddDate");
					String AddMach = jObject.getString("AddMach");
					String RecordId = jObject.getString("RecordId");
					String ASMPreFix = jObject.getString("ASMPreFix");
					

					
					
						
						arrayLength = i;
						ASM_Fasm asAsmList = new ASM_Fasm();
						ASM_FasmDS DS = new ASM_FasmDS(getActivity());
						
						asAsmList.setASM_ASMCODE(ASMCode.toString().trim());
						asAsmList.setASM_ASMNAME(ASMName.toString().trim());
						asAsmList.setASM_RSMCODE(RSMCode.toString().trim());
						asAsmList.setASM_ASMMOB(ASMMob.toString().trim());
						asAsmList.setASM_ASMTEL(ASMTele.toString().trim());
						asAsmList.setASM_ASMMAIL(ASMEMail.toString().trim());
						asAsmList.setASM_STATUS(Status.toString().trim());
						asAsmList.setASM_ADDUSER(AddUser.toString().trim());
						asAsmList.setASM_ADDDATE(AddDate.toString().trim());
						asAsmList.setASM_ADDMACH(AddMach.toString().trim());
						asAsmList.setASM_RECORDID(RecordId.toString().trim());
						asAsmList.setASM_ASMPREFIX(ASMPreFix.toString().trim());
						
						DS.Create(asAsmList);
			
				}
					
				readerfdblist.close();
			} catch (Exception e) 
			{
				// TODO: handle exception
				Log.v("Error", e.toString());
			}
			
	
			
			
			
			return null;
		}
		
		@Override
		protected void onPostExecute(Void args) {
			
			ASMProgressbar1.dismiss();
				//Login();
			showasmdblist();

				
			return;
		}

		
		Handler progressHandler = new Handler()
		{
			public void handleMessage(Message msg) 
			{
				ASMProgressbar1.incrementProgressBy(arrayLength);
			}
		};
		
	}
	
	 public boolean onKeyDown(int keyCode, KeyEvent event) {
	     if(keyCode==KeyEvent.KEYCODE_BACK)
	         Toast.makeText(getActivity(), "Use Home Button",      
	      Toast.LENGTH_LONG).show();

	     return false;
	        // Disable back button..............
	 }
	
}
